<?php
class user_controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();	
		if($this->session->userdata('logged_in'))
   		{
			$this->load->model('user_model');
		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('welcome', 'refresh');
   		}
	}

	public function index()
	{		
		$data['user'] = $this->user_model->get_user();
		$data['title'] = 'user archive';
		
		
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('users/userindex_view', $data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);		
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Username', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('Password', 'Password', 'trim|required|xss_clean');

		if ($this->form_validation->run() === FALSE)
		{
			$this->create_view();
		}
		else
		{
			$this->user_model->set_user();
			$this->userlistview();
		}
	}
	
	public function create_view()
	{	
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('users/usercreate_view', $data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
	
	public function validate_view()
	{
		$userid = $this->input->post('userid');
		$data['user'] = $this->user_model->get_user_id($userid);
		
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('users/uservali_view', $data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
	
	public function validate()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Username', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('Password', 'Password', 'trim|required|xss_clean');

		if ($this->form_validation->run() === FALSE)
		{	
			#redirect('user_controller','refresh');
			$this->validate_view();
		}
		else
		{
			$userid = $this->input->post('userid');
			$this->user_model->validate_user($userid);
			
			$this->userlistview();
		}
		 
	}

	public function userlistview($page = 0)
	{	
		$limit = 5;
		$data['user'] = $this->user_model->get_all($limit, $page * $limit);
		$data['pages'] = ceil($this->user_model->get_count() / $limit);
		
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('users/userlist_view',$data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
}